//
//  DHLoginInfoViewController.m
//  Dresshere
//
//  Created by 张朋 on 2017/12/19.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHLoginInfoViewController.h"
#import "DHLoginStyleViewController.h"
#import "CZHAddressPickerView.h"
#import "AddressPickerHeader.h"

@interface DHLoginInfoViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout;
@property (weak, nonatomic) IBOutlet DHGradualButton *ensureButton;
@property (weak, nonatomic) IBOutlet UIButton *infoButton;
@property (weak, nonatomic) IBOutlet UIButton *closeButton;

@property (strong, nonatomic) NSString *weight;
@property (strong, nonatomic) NSString *height;
@property (strong, nonatomic) NSString *age;

@end

@implementation DHLoginInfoViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (iPhoneX) {
        self.topLayout.constant = 20;
    }
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ECE8EF"];
    
    if ([GlobalManager shareGlobalManager].user.height == 0 && [GlobalManager shareGlobalManager].user.weight == 0  && [GlobalManager shareGlobalManager].user.height == 0) {
        [self.infoButton setTitle:@"身高/体重/年龄" forState:UIControlStateNormal];
    }else{
        [self.infoButton setTitle:[NSString stringWithFormat:@"%ld/%ld/%ld",(long)[GlobalManager shareGlobalManager].user.height,[GlobalManager shareGlobalManager].user.weight,[GlobalManager shareGlobalManager].user.age] forState:UIControlStateNormal];
    }
    
    WeakSelf(weakSelf)
    if (self.userManagerPresent) {
        self.closeButton.hidden = NO;
        [self.closeButton bk_addEventHandler:^(id sender) {
            [weakSelf closeController];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self.ensureButton bk_addEventHandler:^(id sender) {
        if (self.weight && self.height && self.age) {
            [weakSelf updateBody];
        }else{
            [NSObject showHudTipStr:@"请选择身高/体重/年龄"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
     [self.infoButton bk_addEventHandler:^(id sender) {
        [CZHAddressPickerView areaPickerViewWithProvince:@"" city:@"" area:@"" areaBlock:^(NSString *province, NSString *city, NSString *area) {
            self.height = province;
            self.weight = city;
            self.age = area;
            [weakSelf.infoButton setTitle:[NSString stringWithFormat:@"%@/%@/%@",province,city,area] forState:UIControlStateNormal];
        }];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)closeController{
    if (self.userManagerPresent) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [kAppDelegate configHomeController];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)updateBody{
    [[DHNetAPIClient shareDHNetAPIClient] request_bodyWithWeight:self.weight height:self.height age:self.age andBlock:^(id data, NSError *error) {
        if (data) {
            [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                [Login curLoginUser].weight = [self.weight integerValue];
                [Login curLoginUser].height = [self.height integerValue];
                [Login curLoginUser].age = [self.age integerValue];
                [Login doLogin:[Login curLoginUser]];
                [self closeController];
            }];
        }
    }];
}

@end
